<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="2" :sm="12">
            <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item label="查询类别">
              <a-select v-model="queryParam.condition">
                <a-select-option key="GroupName">销售组</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input allowClear @keydown="inputKeyDown" v-model="queryParam.keyword" placeholder="关键字"/>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-range-picker :value="time" @change="onAllocateTimeChange"/>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="24">
            <a-button type="primary" @click="() => {this.pagination.current = 1; this.getDataList()}">查询</a-button>
            <a-button style="margin-left: 8px" @click="repossess()">重置</a-button>
            <a-button style="margin-left: 8px" @click="ExportDataToExcel()" type="primary" v-if="isShowExport">
              <a-icon type="download"/>
              导出数据
            </a-button>
          </a-col>
          <a-col :md="5" :sm="24">
            <a-radio-group
              v-model="sorter.field"
              style="float: right"
              default-value="GroupName"
              @change="getDataList()"
              button-style="solid"
            >
              <a-radio-button value="GroupName">
                <a-icon type="arrow-down"/>&nbsp;销售组&nbsp;
              </a-radio-button>
              <a-radio-button value="客户数">
                <a-icon type="arrow-down"/>&nbsp;客户数&nbsp;
              </a-radio-button>
              <a-radio-button value="期末余额">
                <a-icon type="arrow-down"/>&nbsp;期末余额&nbsp;
              </a-radio-button>
            </a-radio-group>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :scroll="{ y: tableHeight }"
      :columns="columns"
      :rowKey="(row,index) => index"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :bordered="true"
      size="small">
      <span slot="期初余额" slot-scope="期初余额" v-format="'#,##0'">{{ Number(期初余额) }}</span>
      <span slot="本期发货" slot-scope="本期发货" v-format="'#,##0'">{{ Number(本期发货) }}</span>
      <span slot="本期收款" slot-scope="本期收款" v-format="'#,##0'">{{ Number(本期收款) }}</span>
      <span slot="期末余额" slot-scope="期末余额" v-format="'#,##0'">{{ Number(期末余额) }}</span>
      <span slot="呆账" slot-scope="呆账" v-format="'#,##0'">{{ Number(呆账) }}</span>
    </a-table>
  </a-card>
</template>

<script>
import moment from 'moment'
import { download } from '@/components/download'

const columns = [
  { title: '销售组名', dataIndex: 'GroupName', align: 'center', width: '15%' },
  { title: '客户数量', dataIndex: '客户数', align: 'center', width: '10%' },
  { title: '期初余额', dataIndex: '期初余额', align: 'right', width: '15%', scopedSlots: { customRender: '期初余额' } },
  { title: '本期发货', dataIndex: '本期发货', align: 'right', width: '15%', scopedSlots: { customRender: '本期发货' } },
  { title: '本期收款', dataIndex: '本期收款', align: 'right', width: '15%', scopedSlots: { customRender: '本期收款' } },
  { title: '期末余额', dataIndex: '期末余额', align: 'right', width: '15%', scopedSlots: { customRender: '期末余额' } },
  { title: '呆账', dataIndex: '呆账', align: 'right', width: '15%', scopedSlots: { customRender: '呆账' } }
]

export default {
  name: 'ReceivableAccountsSummary',
  components: {},
  mounted () {
    this.time = [moment(this.startTime, 'YYYY-MM-DD'), moment(this.endTime, 'YYYY-MM-DD')]
    this.queryParam.sDate = this.startTime
    this.queryParam.dDate = this.endTime
    this.getDataList()
    this.$nextTick(() => {
      // 页面渲染完成后的回调
      this.tableHeight = this.getTableHeight(true)
    })
  },
  data () {
    return {
      tableHeight: 570, // 表格高度
      data: [],
      pagination: {
        current: 1,
        pageSize: 20,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['20', '30', '40', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'GroupName', order: 'DESC' },
      loading: false,
      columns,
      queryParam: {
        sDate: moment(new Date()).month(new Date().getMonth()).date(1).startOf('month').format('YYYY-MM-DD'),
        dDate: moment(new Date()).month(new Date().getMonth()).date(1).endOf('month').format('YYYY-MM-DD'),
        condition: 'GroupName',
        keyword: ''
      },
      time: null,
      isShowExport: false, // 是否显示导出按钮
      startTime: moment(new Date()).month(new Date().getMonth()).date(1).startOf('month').format('YYYY-MM-DD'),
      endTime: moment(new Date()).month(new Date().getMonth()).date(1).endOf('month').format('YYYY-MM-DD')
    }
  },
  methods: {
    onAllocateTimeChange (dates, dateStrings) {
      this.queryParam.sDate = dateStrings[0]
      this.queryParam.dDate = dateStrings[1]
      this.time = dates
    },
    repossess () {
      this.queryParam = {
        sDate: this.startTime,
        dDate: this.endTime,
        condition: 'GroupName',
        keyword: ''
      }
      this.sorter.field = 'GroupName'
      this.time = [moment(this.startTime, 'YYYY-MM-DD'), moment(this.endTime, 'YYYY-MM-DD')]
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/Base_Manage/Home/GetReceivableAccountsSummary', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
          if (this.data && this.data.length > 0) {
            this.isShowExport = true
          }
        })
    },
    ExportDataToExcel () {
      const that = this
      this.$confirm({
        title: '提示',
        content: '是否导出按当前条件查询的全部记录到Excel ?',
        onOk () {
          that.$http
            .post('/Base_Manage/Home/ExportSummaryToExcel', {
              SortField: that.sorter.field || 'Id',
              SortType: that.sorter.order,
              Search: that.queryParam,
              ...that.filters
            })
            .then(resJson => {
              if (resJson.Success) {
                download(resJson.Data.fileUrl, resJson.Data.fileName)
              } else {
                this.$message.error(resJson.Msg)
              }
            })
        }
      })
    },
    inputKeyDown (e) {
      if (e.keyCode === 13) {
        if (!this.queryParam[0].keyword) return this.$message.error('请输入查询条件')
        this.getDataList()
      }
    },
    timeFormat (val) { // 时间格式化函数
      return moment(val).format('YYYY-MM-DD HH:mm')
    }
  }
}
</script>
